﻿using System;
using System.Drawing;
using System.Windows.Forms;
using WMPLib;
using System.Collections;

namespace Alarm_Clock
{
    public partial class AlarmForm : Form
    {
        Timer t = new Timer();
        int myHour = 0;
        int myMinute = 0;
        int myOffset = 0;
        WMPLib.WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();
        WMPLib.IWMPPlaylist pl;
        ArrayList myPlaylist = new ArrayList();
        bool isMusicPlaying = false;

        public AlarmForm()
        {
            InitializeComponent();
            wmp.uiMode = "none";
            reloadSettings();
        }        


        private void Form1_Load(object sender, EventArgs e)
        {
            t.Interval = 1000;
            t.Tick += new EventHandler(t_Tick);
            t.Start();
            label1.Text = DateTime.Now.ToShortTimeString();
            Point p = new Point();
            p.X = (this.Width / 2) - (label1.Width / 2);
            p.Y = (this.Height / 2) - (label1.Height / 2);
            label1.Location = p;
            this.MouseClick += new MouseEventHandler(AlarmForm_MouseClick);
            wmp.PlayStateChange += new _WMPOCXEvents_PlayStateChangeEventHandler(wmp_PlayStateChange);
        }



        void wmp_PlayStateChange(int NewState)
        {
            if (wmp.playState.Equals(WMPPlayState.wmppsStopped))
            {
                playMusic();
            }
        }



        void AlarmForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip1.Show(e.X, e.Y);
            }
        }



        void t_Tick(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToShortTimeString();
            Point p = new Point();
            p.X = (this.Width / 2) - (label1.Width / 2);
            p.Y = (this.Height / 2) - (label1.Height / 2);
            label1.Location = p;
            

            try
            {
                DateTime alarmTime = new DateTime(1, 1, 1, myHour, myMinute, 0);
                DateTime musicStartTime = new DateTime(1,1,1,myHour, myMinute,0).Subtract(TimeSpan.FromMinutes(myOffset));

                if (DateTime.Now.Hour == musicStartTime.Hour && DateTime.Now.Minute == musicStartTime.Minute && !isMusicPlaying)
                {
                    isMusicPlaying = true;
                    System.Threading.Thread musicThread = new System.Threading.Thread(new System.Threading.ThreadStart(playMusic));
                    musicThread.Start();
                }
            }
            catch (Exception ex)
            {
                lblDebug.Text = ex.ToString();
            }
        }



        private void playMusic()
        {
            wmp.currentPlaylist = pl;
            wmp.play();
        }



        private void closeToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }



        private void setAlarmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSetTime fst = new frmSetTime(this);
            fst.ShowDialog(this);
        }



        public void reloadSettings()
        {
            myPlaylist.Clear();
            System.IO.StreamReader sr = new System.IO.StreamReader("AlarmClock.properties");
            String s = "";
            pl = wmp.playlistCollection.newPlaylist("test");
            while ((s = sr.ReadLine()) != null)
            { 
                String key = s.Substring(0, s.IndexOf(":"));
                String value = s.Substring(s.IndexOf(":") + 1);
                
                switch (key)
                {
                    case "hour":
                        myHour = Int32.Parse(value);
                        break;
                    case "minute":
                        myMinute = Int32.Parse(value);
                        break;
                    case "offset":
                        myOffset = Int32.Parse(value);
                        break;
                    case "playlistItem":
                        pl.appendItem(wmp.newMedia(value));
                        myPlaylist.Add(value);
                        break;
                }
            }
            sr.Close();
        }



        public int hour 
        {
            get { return myHour; }
            set { myHour = value; } 
        }



        public int minute
        {
            get { return myMinute; }
            set { myMinute = value; }
        }



        public int offset
        {
            get { return myOffset; }
            set { myOffset = value; }
        }
    }
}
